def on_gesture_tilt_left():
    global gameMode
    if gameMode == 0:
        soundExpression.slide.play()
        basic.show_leds("""
            . . . . .
            . # . # .
            . . . . .
            # # # # #
            # # . . .
            """)
input.on_gesture(Gesture.TILT_LEFT, on_gesture_tilt_left)

def on_gesture_tilt_right():
    global gameMode
    if gameMode == 0:
        soundExpression.slide.play()
        basic.show_leds("""
            . . . . .
            . # . # .
            . . . . .
            # # # # #
            . . . # #
            """)
input.on_gesture(Gesture.TILT_RIGHT, on_gesture_tilt_right)

def on_button_pressed_a():
    global gameMode
    global isPlaying
    gameMode = 0    # 鬼脸模式  
    isPlaying = False
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_button_pressed_b():
    global gameMode
    global isPlaying
    gameMode = 1    # 唱歌模式
    isPlaying = False
    CloseMouth()
    Singing()
input.on_button_pressed(Button.B, on_button_pressed_b)

def on_sound_loud():
    if isPlaying == True:
        OpenMouth()
input.on_sound(DetectedSound.LOUD, on_sound_loud)

def on_sound_quiet():
    if isPlaying == True:
        CloseMouth()
input.on_sound(DetectedSound.QUIET, on_sound_quiet)

def OpenMouth():
    images.create_image("""
        . # . # .
        . . . . .
        . # # # .
        # . . . #
        . # # # .
        """).show_image(0)

def CloseMouth():
    images.icon_image(IconNames.HAPPY).show_image(0)

def on_logo_touched():
    soundExpression.yawn.play()
    CloseMouth()
input.on_logo_event(TouchButtonEvent.TOUCHED, on_logo_touched)

def Singing():
    global gameMode
    global isPlaying
    if gameMode == 1 and isPlaying == False:
        isPlaying = True
        music.play_tone(330, music.beat(BeatFraction.WHOLE))
        music.play_tone(294, music.beat(BeatFraction.WHOLE))
        music.play_tone(262, music.beat(BeatFraction.WHOLE))
        music.play_tone(294, music.beat(BeatFraction.WHOLE))
        music.play_tone(330, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(330, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(330, music.beat(BeatFraction.WHOLE))
        music.rest(music.beat(BeatFraction.WHOLE))
        music.play_tone(294, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(294, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(294, music.beat(BeatFraction.WHOLE))
        music.rest(music.beat(BeatFraction.WHOLE))
        music.play_tone(330, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(330, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(330, music.beat(BeatFraction.WHOLE))
        music.rest(music.beat(BeatFraction.WHOLE))
        music.play_tone(330, music.beat(BeatFraction.WHOLE))
        music.play_tone(294, music.beat(BeatFraction.WHOLE))
        music.play_tone(262, music.beat(BeatFraction.WHOLE))
        music.play_tone(294, music.beat(BeatFraction.WHOLE))
        music.play_tone(330, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(330, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(330, music.beat(BeatFraction.WHOLE))
        music.play_tone(262, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(294, music.beat(BeatFraction.HALF))
        music.rest(music.beat(BeatFraction.HALF))
        music.play_tone(294, music.beat(BeatFraction.WHOLE))
        music.play_tone(330, music.beat(BeatFraction.WHOLE))
        music.play_tone(294, music.beat(BeatFraction.WHOLE))
        music.play_tone(262, music.beat(BeatFraction.BREVE))
        isPlaying = False
        CloseMouth()
        
isPlaying = False
gameMode = 0    # 0：鬼脸模式，1：唱歌模式
music.set_tempo(120)
images.icon_image(IconNames.HAPPY).show_image(0)